package com.paireach.bw.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.paireach.bw.application.MyApplication;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.BaseEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.utils.ToastUtil;
import com.paireach.bw.utils.ToastyUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import es.dmoral.toasty.Toasty;


/**
 * use eventbus to initial basefragment
 */
public class BaseFragment extends Fragment {

    public Context context;
    public String phoneNumber;
    public String name;
    public String driverNum;//车牌号

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        phoneNumber = MyApplication.getSpUtils().getString(Constant.TEL, "");
        name = MyApplication.getSpUtils().getString(Constant.REAL_NAME, "");
        driverNum = MyApplication.getSpUtils().getString(Constant.DRIVER_NUM, "");
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public final void onEventCallBack(BaseEvent event) {
        if (event instanceof HttpErrorEvent) {
            //mark error
            httpErrorEvent((HttpErrorEvent) event);
        } else if (event instanceof HttpSuccessEvent) {
            httpSuccessEvent((HttpSuccessEvent) event);
        } else {
            applicationEvent((AppEvent) event);
        }
    }

    /**
     * 处理网络失败/错误请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 错误事件
     */
    public void httpErrorEvent(HttpErrorEvent event) {

    }

    /**
     * 处理网络成功请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 成功事件
     */
    public void httpSuccessEvent(HttpSuccessEvent event) {

    }

    /**
     * 处理app内部事件
     *
     * @param event app内部事件
     */
    public void applicationEvent(AppEvent event) {

    }

    public void showToast(String message) {
        ToastUtil.showShortToast(context, message);
    }


    public void showProgressDialog() {
        try {
            ((BaseActivity) getActivity()).showProgressDialog();
        } catch (ClassCastException e) {
            Log.w("BaseFragment", "trying to show progress dialog failed.Parent activity:" + getActivity().getClass().getName() + " never extends BaseActivity!");
        } catch (NullPointerException e) {
            Log.w("BaseFragment", getClass().getName() + "does not attach to activity");
        }
    }

    public void dismissProgressDialog() {
        try {
            ((BaseActivity) getActivity()).dismissProgressDialog();
        } catch (ClassCastException e) {
            Log.w("BaseFragment", "trying to dismiss progress dialog failed.Parent activity:" + getActivity().getClass().getName() + " never extends BaseActivity!");
        } catch (NullPointerException e) {
            Log.w("BaseFragment", getClass().getName() + "does not attach to activity");
        }
    }


}
